//
//  NewsCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/19.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class NewsCell: UITableViewCell {
    
    var model : NewsModel! {
        
        didSet{
            updateData()
        }
    }
    
    fileprivate let newsImageView = UIImageView()
    fileprivate let titleLabel = UILabel()
//    fileprivate let contextLabel = UILabel()
    fileprivate let timeLabel = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.mineColor()
 
        newsImageView.layer.cornerRadius = RATIO_H(maxNum: 10)
        newsImageView.layer.masksToBounds = true
        self.contentView.addSubview(newsImageView)
        
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        titleLabel.numberOfLines = 2
        self.contentView.addSubview(titleLabel)
        
//        contextLabel.textColor = UIColor.lightGray
//        contextLabel.numberOfLines = 2
//        contextLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
//        self.contentView.addSubview(contextLabel)
        
//        timeLabel.textAlignment = .right
        timeLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        timeLabel.textColor = UIColor.gray
        self.contentView.addSubview(timeLabel)
        
        let whiteView = UIView()
        whiteView.backgroundColor = UIColor.white
        self.contentView.addSubview(whiteView)
        
        newsImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.centerY.equalToSuperview()
            make.width.equalTo(RATIO(maxNum: 110))
            make.height.equalTo(RATIO_H(maxNum: 70))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(newsImageView)
            make.left.equalTo(newsImageView.snp.right).offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.height.greaterThanOrEqualTo(10)
        }
        
//        contextLabel.snp.makeConstraints { (make) in
//            make.left.equalTo(titleLabel)
//            make.top.equalTo(titleLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
//            make.right.equalTo(titleLabel)
//            make.height.equalTo(RATIO_H(maxNum: 40))
//        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.right.equalTo(titleLabel)
            make.bottom.equalTo(newsImageView)
            make.height.greaterThanOrEqualTo(10)
        }
        
        whiteView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 5))
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        
    }
    
    //MARK: 刷新数据
    fileprivate func updateData() {
        
        newsImageView.setUrlImage(with: kmHttps + (model.image ?? ""), placeholder: UIImage.imageWithName("loading"))
        titleLabel.text = model.title
//        contextLabel.text = model.text
        timeLabel.text = model.time
    }

}
